﻿@page "/tree-grid/expando-object"

@using Syncfusion.Blazor.TreeGrid
@using Syncfusion.Blazor.Grids
@using Syncfusion.Blazor.DropDowns
@using System.Dynamic;
@*Hidden:Lines*@
@using BlazorDemos
@inherits SampleBaseComponent;
@*End:Hidden*@
<SampleDescription>
    <p>This sample demonstrates the usage of ExpandoObject (dynamic) data binding with CRUD operations, sorting, filtering functionalities in Tree Grid component with ExpandoObject.</p>
</SampleDescription>
<ActionDescription>
    <p>More information about ExpandoObject can be found in this <a target='_blank' href='https://blazor.syncfusion.com/documentation/treegrid/expandoobject-binding/'>DataSource</a> section.</p>
    <p>In this demo, we have bound the dynamic list of objects as dataSource to Tree Grid using the ExpandoObjects.</p>
</ActionDescription>

<div class="col-lg-12 control-section">
    <div class="content-wrapper">
        <div class="row">
            <SfTreeGrid DataSource="@TreeData" @ref="TreeGrid" AllowPaging="true" IdMapping="TaskID" ParentIdMapping="ParentID" TreeColumnIndex="1" AllowFiltering="true" AllowSorting="true" Toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })">
                <TreeGridPageSettings PageSize="2"></TreeGridPageSettings>
                <TreeGridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="Syncfusion.Blazor.TreeGrid.EditMode.Cell"></TreeGridEditSettings>
                <TreeGridColumns>
                    <TreeGridColumn Field="TaskID" HeaderText="Task ID" ValidationRules="@(new ValidationRules() { Required = true, Number = true })" Width="80" IsPrimaryKey="true" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></TreeGridColumn>
                    <TreeGridColumn Field="TaskName" HeaderText="Task Name" ValidationRules="@(new ValidationRules() { Required = true })" Width="100"></TreeGridColumn>
                    <TreeGridColumn Field="StartDate" HeaderText="StartDate" Format="d" Type=Syncfusion.Blazor.Grids.ColumnType.Date EditType=Syncfusion.Blazor.Grids.EditType.DatePickerEdit  Width="100" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></TreeGridColumn>
                    <TreeGridColumn Field="Duration" HeaderText="Duration" ValidationRules="@(new ValidationRules() { Number = true, Min = 0 })" Width="80" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></TreeGridColumn>
                    <TreeGridColumn Field="Progress" HeaderText="Progress" ValidationRules="@(new ValidationRules() {Required = true })" Width="80">
                        <FilterTemplate>
                            <SfDropDownList TValue="string" TItem="DDLData" DataSource="@DropdownData">
                                <DropDownListEvents ValueChange="Change" TItem="DDLData" TValue="string"></DropDownListEvents>
                                <DropDownListFieldSettings Text="Text" Value="Value"></DropDownListFieldSettings>
                            </SfDropDownList>
                        </FilterTemplate>
                    </TreeGridColumn>
                    <TreeGridColumn Field="Priority" HeaderText="Priority" ValidationRules="@(new ValidationRules() { Required = true })" Width="80"></TreeGridColumn>
                </TreeGridColumns>
            </SfTreeGrid>
        </div>
    </div>
</div>

@code {

    SfTreeGrid<ExpandoObject> TreeGrid;

    public List<ExpandoObject> TreeData { get; set; }

    public string FilterValue { get; set; } = "All";

    public List<DDLData> DropdownData { get; set; } = new List<DDLData> {
        new DDLData { Text = "Open", Value = "Open" },
        new DDLData { Text = "Validated", Value = "Validated" },
        new DDLData { Text = "Closed", Value = "Closed" },
        new DDLData { Text = "All", Value = "All" }
};

    protected override void OnInitialized()
    {
        this.TreeData = GetData().ToList();
    }
    
    public static List<ExpandoObject> Data = new List<ExpandoObject>();

    public static int ParentRecordID { get; set; }

    public static int ChildRecordID { get; set; }


    public static List<ExpandoObject> GetData()
    {
        Data.Clear();
        ParentRecordID = 0;
        ChildRecordID = 0;
        
        for (var i = 1; i <= 60; i++)
        {
            Random ran = new Random();
            DateTime start = new DateTime(1992, 06, 07);
            int range = (DateTime.Today - start).Days;
            DateTime startingDate = start.AddDays(ran.Next(range));
            dynamic ParentRecord = new ExpandoObject();
            ParentRecord.TaskID = ++ParentRecordID;
            ParentRecord.TaskName = "Parent Task " + i;
            ParentRecord.StartDate = startingDate;
            ParentRecord.Progress = ParentRecordID % 2 == 0 ? "In Progress" : "Open";
            ParentRecord.Priority = ParentRecordID % 2 == 0 ? "High" : "Low";
            ParentRecord.Duration = ParentRecordID % 2 == 0 ? 32 : 76;
            ParentRecord.ParentID = null;
            Data.Add(ParentRecord);
            AddChildRecords(ParentRecordID);
            }
            return Data;
        }
            
    public static void AddChildRecords(int ParentId)
    {
        for (var i = 1; i < 4; i++)
        {
            Random ran = new Random();
            DateTime start = new DateTime(1992, 06, 07);
            int range = (DateTime.Today - start).Days;
            DateTime startingDate = start.AddDays(ran.Next(range));
            dynamic ChildRecord = new ExpandoObject();
            ChildRecord.TaskID = ++ParentRecordID;
            ChildRecord.TaskName = "Child Task " + ++ChildRecordID;
            ChildRecord.StartDate = startingDate;
            ChildRecord.Progress = ParentRecordID % 3 == 0 ? "Validated" : "Closed";
            ChildRecord.Priority = ParentRecordID % 3 == 0 ? "Low" : "Critical";
            ChildRecord.Duration = ParentRecordID % 3 == 0 ? 64 : 98;
            ChildRecord.ParentID = ParentId;
            Data.Add(ChildRecord);
            }
        }

    public class DDLData
    {
        public string Text { get; set; }
        public string Value { get; set; }
    }

    public async Task Change(Syncfusion.Blazor.DropDowns.ChangeEventArgs<string, DDLData> Args)
    {
        if (Args.Value == "All")
        {
            await this.TreeGrid.ClearFilteringAsync();
        }
        else
        {
            await this.TreeGrid.FilterByColumnAsync("Progress", "equal", Args.Value);
        }
    }
}
